package com.example.tliaswebmanagement.controller;

import com.example.tliaswebmanagement.Annotation.LogOperation;
import com.example.tliaswebmanagement.entity.Emp;
import com.example.tliaswebmanagement.entity.PageResult;
import com.example.tliaswebmanagement.entity.Result;
import com.example.tliaswebmanagement.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpController {
    private final EmpService empService;


    @Autowired
    public EmpController(EmpService empService) {
        this.empService = empService;
    }

    //获取所有员工信息
    @GetMapping
    public Result findAll(@RequestParam(defaultValue = "1")Integer page,
                          @RequestParam(defaultValue = "10") Integer pageSize,
                          String name, Short gender,
                          @DateTimeFormat(pattern = "yyyy-MM-dd")LocalDate begin,
                          @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end)
    {
        log.info("查询员工信息，参数：{},{},{},{},{},{}",page,pageSize,name,gender,begin,end);
        PageResult pr=empService.findAll(page,pageSize,name,gender,begin,end);
        return Result.success(pr);
    }

    @LogOperation
    //插入新建员工基本信息
    @PostMapping
    public Result Insert(@RequestBody Emp emp) {
        log.info("新增员工，基本员工信息：{}", emp);
        empService.insert(emp);
        return Result.success();
    }

    //删除员工信息
    @DeleteMapping
    //这是封装到集合中，需要加注解，如果封装到Integer数组中，则不需要注解
    public Result deleteById(@RequestParam List<Integer> ids)
    {
        log.info("删除员工信息，员工id：{}", ids);
        empService.deleteById(ids);
        return Result.success();
    }

    //修改员工,数据回显
    @GetMapping("/{id}")
    public Result getInfoById(@PathVariable Integer id) {
        log.info("数据回显，员工id{}",id);
        Emp emp=empService.getInfoById(id);
        return Result.success(emp);
    }
    //修改员工，更新数据
    @PutMapping
    public Result update(@RequestBody Emp emp) {
        log.info("更新员工信息，员工信息：{}", emp);
        empService.update(emp);
        return Result.success();
    }

    //获取全部员工信息
    @GetMapping("/list")
    public Result getAll() {
        log.info("获取全部员工信息");
        List<Emp> list = empService.getAll();
        return Result.success(list);
    }
}
